package com.app.utils.base.http;

import com.app.utils.base.j2se.JsonUtil;
import com.app.utils.base.j2se.Logger;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;

/**
 * Description
 * Created by 王伟
 * Date create on 17:52 2019/11/19.
 */
public class ResponseHandler {

	/**
	 * 输出返回消息
	 */
	public static void write(Map<String, Object> msg, HttpServletRequest request, HttpServletResponse response){
		write(JsonUtil.toJson(msg), request, response);
	}
	
	/**
	 * 输出返回消息
	 */
	public static void write(String string, HttpServletRequest request, HttpServletResponse response){
		try {
			string = string.replaceAll("[\\x00-\\x1F]", "_");
			response.setCharacterEncoding("utf-8");
			response.setContentType("application/json");
			PrintWriter writer = response.getWriter();
			writer.print(string);
			writer.flush();
		} catch (IOException e) {
			Logger.error("响应助手输出返回消息", e);
		}
	}
	
}
